﻿using System.Net;
using BusinessFacade;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Orleans;
using Orleans.Configuration;
using Orleans.Hosting;

namespace HostApp
{
    internal class Program
    {
        static async Task Main(string[] args)
        {
            IHostBuilder builder = new HostBuilder().UseOrleans(builder =>
            {
                builder.UseLocalhostClustering()
                    .Configure<EndpointOptions>(options =>
                    {
                        // Silo 和 Gateway 的 IP 和端口配置
                        options.SiloPort = 11111;              // Silo 通信端口
                        options.GatewayPort = 30000;           // 客户端连接网关端口
                        options.AdvertisedIPAddress = IPAddress.Parse("192.168.176.127"); // 公网 IP 或内网可达 IP
                    })
                    .Configure<ClusterOptions>(options =>
                    {
                        options.ClusterId = "dev";
                        options.ServiceId = "HelloWorldApp";
                    })
                    .Configure<EndpointOptions>(
                        options => options.AdvertisedIPAddress = IPAddress.Loopback)
                    //.ConfigureApplicationParts(
                    //    parts =>
                    //    {
                    //        parts.AddApplicationPart(typeof(ITestCommon).Assembly).WithReferences();
                    //        parts.AddApplicationPart(typeof(TestCommon).Assembly).WithReferences();
                    //    })
                    .ConfigureLogging(logging => logging.AddConsole());
            });

            var host = builder.Build();
            await host.StartAsync();

            Console.WriteLine("Orleans Silo Started.");
            Console.ReadKey();
            // Stop the host
            await host.StopAsync();
        }
    }
}

//var client = host.Services.GetRequiredService<IClusterClient>();
//var grain = client.GetGrain<ITestCommon>(0);
////Console.Clear();
//// Call a method on the grain
//var response = await grain.GetString("熊二", "熊大光頭強又來砍樹啦！");
//Console.WriteLine(response);